home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Toolbox / icon cache demo / Source / icon cache.c
Encoding:
Text File  |  1996-09-17  |  5.0 KB  |  232 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    This sample demonstrates the use of an icon cache
  3.     //    to limit the search for icon resource to one resource
  4.     //    file. It does this by installing an icon getter function
  5.     //    into the cache which calls Get1(Ind)Resource instead of the
  6.     //    usual GetResource.
  7.     //
  8.     //    The application is meant to display the first
  9.     //    suite produced by an indexed resource call. There's nothing
  10.     //    stopping you from calling Get1Resource or anything else
  11.     //    which might produce a handle to a member of an icon suite.
  12.     //
  13.     //    There's also some jiggery-pokery having to do with my
  14.     //    distaste for purgeable handles; see below for details.
  15.     //
  16.     //    Complaints and kudos to:
  17.     //
  18.     //        Pete Gontier
  19.     //        Apple Macintosh Developer Technical Support
  20.     //        <gurgle@apple.com>
  21.     //
  22.     
  23.     //updates 8/96: IconCache.h added as prefix file for 68K and PPC MC projects
  24.     //line 154: PPC project has line 1111 exception
  25.  
  26. #define OLDROUTINELOCATIONS        0
  27. #define OLDROUTINENAMES            0
  28. #define SystemSevenOrLater        1
  29.  
  30. #ifndef __FONTS__
  31. #    include <Fonts.h>
  32. #endif
  33.  
  34. #ifndef __DIALOGS__
  35. #    include <Dialogs.h>
  36. #endif
  37.  
  38. #ifndef __RESOURCES__
  39. #    include <Resources.h>
  40. #endif
  41.  
  42. #ifndef __STANDARDFILE__
  43. #    include <StandardFile.h>
  44. #endif
  45.  
  46. #ifndef __ICONS__
  47. #    include <Icons.h>
  48. #endif
  49.  
  50. static pascal Handle MyIconGetter (ResType rt, void *)
  51. {
  52.     //
  53.     //    DisposeIconSuite assumes resources are purgeable and doesn't
  54.     //    release them. Since we would rather the memory be freed,
  55.     //    we have our icon getter function ('MyIconGetter') detach
  56.     //    each resource it gets. This turns off the resource bit for
  57.     //    the handle and clues DisposeIconSuite it should call DisposeHandle.
  58.     //
  59.  
  60.     Handle iconH = Get1IndResource (rt,1);
  61.     if (!iconH || ResError ( ))
  62.         return nil;
  63.  
  64.     DetachResource (iconH);
  65.     if (ResError ( ))
  66.     {
  67.         ReleaseResource (iconH);
  68.         return nil;
  69.     }
  70.  
  71.     return iconH;
  72. }
  73.  
  74. static pascal void Plot1IndIconSuite (Rect *rect)
  75. {
  76.     Handle iconCacheH;
  77.  
  78.     static IconGetterUPP iconGetterUPP;
  79.  
  80.     if (!iconGetterUPP)
  81.         iconGetterUPP = NewIconGetterProc (MyIconGetter);
  82.     if (!iconGetterUPP)
  83.         return;
  84.     if (!MakeIconCache (&iconCacheH,iconGetterUPP,nil))
  85.     {
  86.         PlotIconSuite (rect,kAlignNone,kTransformNone,iconCacheH);
  87.  
  88.         //
  89.         //    DisposeIconSuite assumes resources are purgeable and doesn't
  90.         //    release them. Since we would rather the memory be freed,
  91.         //    we have our icon getter function ('MyIconGetter') detach
  92.         //    each resource it gets. This turns off the resource bit for
  93.         //    the handle and clues DisposeIconSuite it should call DisposeHandle.
  94.         //
  95.  
  96.         DisposeIconSuite (iconCacheH,true);
  97.     }
  98. }
  99.  
  100. //////////////////////////////////////////////////////////////////////
  101. //
  102. //    Below please find the usual sort of application boilerplate.
  103. //
  104. //////////////////////////////////////////////////////////////////////
  105.  
  106. static Boolean gQuitting;
  107.  
  108. static pascal OSErr InitMac (void)
  109. {
  110.     MaxApplZone ( );
  111.     InitGraf (&(qd.thePort));
  112.     InitFonts ( );
  113.     InitWindows ( );
  114.     InitMenus ( );
  115.     TEInit ( );
  116.     InitDialogs (nil);
  117.  
  118.     return noErr;
  119. }
  120.  
  121. static pascal void IconUserItem (WindowRef dlgRef, short itemNo)
  122. {
  123.     short crf = CurResFile ( );
  124.     short iType; Handle iHandle; Rect iRect;
  125.     GetDialogItem (dlgRef,itemNo,&iType,&iHandle,&iRect);
  126.     UseResFile (GetWRefCon (dlgRef));
  127.     if (!ResError ( ))
  128.     {
  129.         Plot1IndIconSuite (&iRect);
  130.         UseResFile (crf);
  131.     }
  132. }
  133.  
  134. static pascal void GrabIcon (void)
  135. {
  136.     StandardFileReply sfr;
  137.     StandardGetFile (nil,-1,nil,&sfr);
  138.     if (sfr.sfGood)
  139.     {
  140.         short crf = CurResFile ( );
  141.         short resRefNum = FSpOpenResFile (&(sfr.sfFile),fsRdPerm);
  142.         if (resRefNum != -1)
  143.         {
  144.             DialogRef dlgRef = nil;
  145.  
  146.             UseResFile (crf);
  147.  
  148.             dlgRef = GetNewDialog (128,nil,(WindowRef)-1);
  149.             if (dlgRef)
  150.             {
  151.                 short itemHit;
  152.                 short iType; Handle iHandle; Rect iRect;
  153.                 static UserItemUPP userItemUPP;
  154.  
  155.                 if (!userItemUPP)
  156.                     userItemUPP = NewUserItemProc (IconUserItem);
  157.                 if (userItemUPP)
  158.                 {
  159.                     GetDialogItem (dlgRef,2,&iType,&iHandle,&iRect);
  160.                     SetDialogItem (dlgRef,2,iType,(Handle)userItemUPP,&iRect);
  161.                     SetWRefCon (dlgRef,resRefNum);
  162.  
  163.                     ShowWindow (dlgRef);
  164.                     ModalDialog ( NewModalFilterProc(nil),&itemHit);
  165.                 }
  166.                 DisposeDialog (dlgRef);
  167.             }
  168.             
  169.             CloseResFile (resRefNum);
  170.         }
  171.     }
  172. }
  173.  
  174. static pascal void Command (long ms)
  175. {
  176.     short    menuID = ms >> 16,
  177.             menuItem = ms;
  178.  
  179.     if (menuID == 129)
  180.     {
  181.         if (menuItem == 2)
  182.             gQuitting = true;
  183.         else if (menuItem == 1)
  184.             GrabIcon ( );
  185.     }
  186. }
  187.  
  188. static pascal void HandleEvent (const EventRecord *eventP)
  189. {
  190.     if (eventP->what == mouseDown)
  191.     {
  192.         WindowRef whichWindow;
  193.         short partCode = FindWindow (eventP->where, &whichWindow);
  194.         if (partCode == inMenuBar)
  195.         {
  196.             long ms = MenuSelect (eventP->where);
  197.             if (ms) Command (ms);
  198.             HiliteMenu (0);
  199.         }
  200.     }
  201. }
  202.  
  203. static pascal Boolean SetUpMenuBar (void)
  204. {
  205.     Boolean result = false;
  206.     Handle mBar = GetNewMBar (128);
  207.     if (!ResError ( ) && mBar)
  208.     {
  209.         SetMenuBar (mBar);
  210.         AppendResMenu (GetMenuHandle (130), 'DRVR');
  211.         DrawMenuBar ( );
  212.         result = true;
  213.         ReleaseResource (mBar);
  214.     }
  215.     return result;
  216. }
  217.  
  218. void main (void)
  219. {
  220.     if (!InitMac ( ) && SetUpMenuBar ( ))
  221.     {    
  222.         do
  223.         {
  224.             EventRecord event;
  225.             InitCursor ( );
  226.             WaitNextEvent (everyEvent, &event, -1, nil);
  227.             HandleEvent (&event);
  228.         }
  229.         while (!gQuitting);
  230.     }
  231. }
  232.